package com.service.weChatWifiLogin.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.service.config.utils.ModelMapperUtil;
import com.service.config.utils.Result;
import com.service.weChatWifiLogin.dto.ReviewInfoDto;
import com.service.weChatWifiLogin.dto.ReviewRecordInfoDto;
import com.service.weChatWifiLogin.entity.ReviewRecordInfo;
import com.service.weChatWifiLogin.dao.ReviewRecordInfoDao;
import com.service.weChatWifiLogin.service.ReviewRecordInfoService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * (ReviewRecordInfo)表服务实现类
 *
 * @author makejava
 * @since 2021-06-29 14:42:44
 */
@Service("reviewRecordInfoService")
public class ReviewRecordInfoServiceImpl implements ReviewRecordInfoService {
    @Resource
    private ReviewRecordInfoDao reviewRecordInfoDao;

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public ReviewRecordInfo queryById(String id) {
        return this.reviewRecordInfoDao.queryById(id);
    }

    /**
     * 查询多条数据
     *
     * @param offset 查询起始位置
     * @param limit  查询条数
     * @return 对象列表
     */
    @Override
    public List<ReviewRecordInfo> queryAllByLimit(int offset, int limit) {
        return this.reviewRecordInfoDao.queryAllByLimit(offset, limit);
    }

    /**
     * 新增数据
     *
     * @param reviewRecordInfo 实例对象
     * @return 实例对象
     */
    @Override
    public ReviewRecordInfo insert(ReviewRecordInfo reviewRecordInfo) {
        this.reviewRecordInfoDao.insert(reviewRecordInfo);
        return reviewRecordInfo;
    }

    /**
     * 修改数据
     *
     * @param reviewRecordInfo 实例对象
     * @return 实例对象
     */
    @Override
    public ReviewRecordInfo update(ReviewRecordInfo reviewRecordInfo) {
        this.reviewRecordInfoDao.update(reviewRecordInfo);
        return this.queryById(reviewRecordInfo.getId());
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(String id) {
        return this.reviewRecordInfoDao.deleteById(id) > 0;
    }

    @Override
    public List<ReviewRecordInfo> queryAllByShopIdAndLimit(String shopId) {
        return reviewRecordInfoDao.queryAllByShopIdAndLimit(shopId,0,100);
    }

    @Override
    public Result queryAll(ReviewRecordInfo reviewRecordInfo,Integer pageSize ,Integer pageNum ) throws ParseException {
        List<ReviewRecordInfoDto> reviewRecordInfoDtoList = new ArrayList<>();
        PageHelper.startPage(pageNum,pageSize*4);
        PageInfo<ReviewRecordInfo> pageInfo = new PageInfo<>();
        if (reviewRecordInfo.getVersionNo() != null && !"".equals(reviewRecordInfo.getVersionNo())
                && reviewRecordInfo.getReviewerContent() != null &&  !"".equals(reviewRecordInfo.getReviewerContent())) {
            List<ReviewRecordInfo> reviewRecordInfoList = reviewRecordInfoDao.queryByDate(reviewRecordInfo, reviewRecordInfo.getVersionNo(), reviewRecordInfo.getReviewerContent());
            pageInfo = new PageInfo<>(reviewRecordInfoList);
            return getResult(pageInfo,reviewRecordInfoDtoList, pageInfo.getList(),pageSize);
        }else {
            pageInfo = new PageInfo<>(reviewRecordInfoDao.queryAll(reviewRecordInfo));
            return getResult(pageInfo,reviewRecordInfoDtoList, pageInfo.getList(),pageSize);
        }
    }

    /**
     * 分组
     * @param reviewRecordInfoDtoList
     * @param reviewRecordInfoList
     * @return
     */
    private Result getResult(PageInfo pageInfo,List<ReviewRecordInfoDto> reviewRecordInfoDtoList, List<ReviewRecordInfo> reviewRecordInfoList,Integer pageSize) {
        HashMap map = new HashMap();
        Map<String, List<ReviewRecordInfo>> collect = reviewRecordInfoList.stream().collect(Collectors.groupingBy(ReviewRecordInfo::getVersionNo));
        collect.entrySet().forEach(stringListEntry -> {
            ReviewRecordInfoDto reviewRecordInfoDto = new ReviewRecordInfoDto();
            List<ReviewRecordInfo> value = stringListEntry.getValue();
            reviewRecordInfoDto.setShopId(value.get(0).getShopId());
            if (value.get(0).getReviewer() != null && !"".equals(value.get(0).getReviewer())) {
                reviewRecordInfoDto.setReviewer(value.get(0).getReviewer());
            }
            if (value.get(0).getReviewerContent() != null && !value.get(0).getReviewerContent().equals("")) {
                reviewRecordInfoDto.setReviewerContent(value.get(0).getReviewerContent());
            }
            List<ReviewInfoDto> reviewInfoDtoList = new ArrayList<>();
            List<ReviewInfoDto> reviewInfoDtos = ModelMapperUtil.strictMapList(value, ReviewInfoDto.class);
            List<ReviewInfoDto> reviewInfoDtoList1 = reviewInfoDtos.stream().filter(reviewInfoDto -> reviewInfoDto.getReviewerColumnDesc().equals("店铺名称")).collect(Collectors.toList());
            reviewInfoDtoList.add(reviewInfoDtoList1.get(0));
            reviewInfoDtoList1 = reviewInfoDtos.stream().filter(reviewInfoDto -> reviewInfoDto.getReviewerColumnDesc().equals("wifi名称")).collect(Collectors.toList());
            reviewInfoDtoList.add(reviewInfoDtoList1.get(0));
            reviewInfoDtoList1 = reviewInfoDtos.stream().filter(reviewInfoDto -> reviewInfoDto.getReviewerColumnDesc().equals("logo图片")).collect(Collectors.toList());
            reviewInfoDtoList.add(reviewInfoDtoList1.get(0));
            reviewInfoDtoList1 = reviewInfoDtos.stream().filter(reviewInfoDto -> reviewInfoDto.getReviewerColumnDesc().equals("营销图片")).collect(Collectors.toList());
            reviewInfoDtoList.add(reviewInfoDtoList1.get(0));
            reviewRecordInfoDto.setReviewInfo(reviewInfoDtoList);
            reviewRecordInfoDto.setVersionNo(value.get(0).getVersionNo()); // 审核批号
            reviewRecordInfoDto.setReviewStatus(value.get(0).getReviewStatus());
            reviewRecordInfoDtoList.add(reviewRecordInfoDto);
        });
        long total = pageInfo.getTotal()/4;
        if (total % pageSize > 0 ) {
            map.put("pages",total/pageSize+1);
        }else {
            map.put("pages",total/pageSize);
        }
        map.put("total",total);
        map.put("reviewRecordInfoDtoList",reviewRecordInfoDtoList);
        return Result.ok().setData(map);
    }
}
